<script setup>
import { onLaunch } from "@dcloudio/uni-app";
import { onMounted } from "vue";
import useUserStore from '@/store/modules/user'
import Web3 from "web3";
const userStore = useUserStore();
onLaunch(()=> {
  // 如果没有签名就去连接钱包
  if (!userStore.isSign) {
    userStore.connectWallet();
  }
  // 监听切换钱包
  ethereum.on("accountsChanged", async (accounts) => {
    accountsChanged();
  });
});
onMounted(() => {
  accountsChanged();
})
  // 切换钱包操作
const accountsChanged = async () => {
  const web3 = new Web3(window.ethereum);
  const accounts = await web3.eth.getAccounts();
  const ownerAddress = accounts[0].toLowerCase();
  if (ownerAddress != userStore.address && userStore.address != '') {
    uni.clearStorageSync();
    userStore.reset();
    uni.reLaunch({
      url: "/pages/home/home",
    });
  }
}
</script>

<style lang="scss">
@import '@/uni.scss';
</style>
